from django.db import models


# Create your models here.

class BaseModel(models.Model):
    create_time = models.DateTimeField('创建时间', auto_now_add=True)
    update_time = models.DateTimeField('更新时间', auto_now=True)

    class Meta:
        # 防止生成表
        abstract = True


class Project(BaseModel):
    name = models.CharField('项目名称', max_length=30, unique=True)
    project_owner = models.CharField('项目负责人', max_length=20, null=True, blank=True)
    tester = models.CharField('测试人员', max_length=100)
    developer = models.CharField('开发人员', max_length=100, null=True, blank=True)
    project_desc = models.CharField('项目描述', max_length=300, null=True, blank=True)

    class Meta:
        verbose_name = "项目"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Module(BaseModel):
    project = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name="项目")
    name = models.CharField('模块名称', max_length=30, null=True, blank=True)
    tester = models.CharField('测试人员', max_length=100, null=True, blank=True)
    developer = models.CharField('开发人员', max_length=100, null=True, blank=True)
    project_desc = models.CharField('项目描述', max_length=300, null=True, blank=True)

    class Meta:
        verbose_name = "模块"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class TestCase(BaseModel):
    LEVEL_CHOICE = (
        (0, "smoke"),
        (1, "P0"),
        (2, "P1"),
        (3, "P2"),
    )

    project = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name="项目")
    module = models.ForeignKey(Module, on_delete=models.CASCADE, verbose_name="模块")
    name = models.CharField(max_length=30, verbose_name="用例名称")
    run_config = models.TextField(verbose_name="运行配置")
    request_info = models.TextField(verbose_name="请求信息")
    level = models.IntegerField(verbose_name="优先级", choices=LEVEL_CHOICE)
    steps = models.TextField(verbose_name="用例前置步骤")
    author = models.CharField(max_length=20, verbose_name="创建人")

    class Meta:
        verbose_name = "用例"
        verbose_name_plural = verbose_name
        db_table = 'TestCase'

    def __str__(self):
        return self.name


class TestSuit(BaseModel):
    project = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name="项目")
    name = models.CharField(max_length=30, verbose_name="套件名称")
    cases = models.TextField(verbose_name="用例列表")
    author = models.CharField(max_length=20, verbose_name="创建人")

    class Meta:
        verbose_name = "套件"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class RunEnv(BaseModel):
    project = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name="项目")
    module = models.ForeignKey(Module, on_delete=models.CASCADE, verbose_name="模块")
    name = models.CharField(max_length=50, verbose_name="环境名称")
    host = models.CharField(max_length=50, verbose_name="环境host")
    desc = models.CharField(max_length=200, verbose_name="环境描述")

    class Meta:
        verbose_name = "运行环境"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class RunConfig(BaseModel):
    project = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name="项目")
    module = models.ForeignKey(Module, on_delete=models.CASCADE, verbose_name="模块")
    name = models.CharField(max_length=50, verbose_name="配置名称")
    variables = models.TextField(verbose_name="配置变量", null=True, blank=True)
    hooks = models.CharField(max_length=1024, verbose_name="钩子函数", null=True, blank=True)
    whole_request = models.CharField(max_length=1024, verbose_name="用例请求信息", null=True, blank=True)
    headers = models.CharField(max_length=1024, verbose_name="请求headers", null=True, blank=True)
    cookies = models.CharField(max_length=1024, verbose_name="请求cookies", null=True, blank=True)
    author = models.CharField(max_length=20, verbose_name="创建人")

    class Meta:
        verbose_name = "运行配置"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class AuxiScript(BaseModel):
    project = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name="项目")
    script_info = models.TextField(verbose_name="函数信息")

    class Meta:
        verbose_name = "辅助函数"
        verbose_name_plural = verbose_name


class TestReport(BaseModel):
    name = models.CharField(max_length=50, verbose_name="报告名称")
    case_nums = models.IntegerField(default=0, verbose_name="执行用例数")
    success_nums = models.IntegerField(default=0, verbose_name="执行成功数")
    report_info = models.TextField(verbose_name="报告详情")

    class Meta:
        verbose_name = "测试报告"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

